﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Windows.Forms;

namespace GraphsLab5
{
    public partial class Form1 : Form
    {
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (!mode)
            {
                if (!isDrawn) return;
                int x = e.X;
                int y = e.Y;
                for (int i = 0; i < points.Count; i++)
                {
                    int predicate = (x - points[i].X) * (x - points[i].X) + (y - points[i].Y) * (y - points[i].Y);
                    if (predicate <= 100)
                    {
                        mouseDown = true;
                        pointMove = i;
                        return;
                    }
                }
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isDrawn) return;
            if (mouseDown)
            {
                points[pointMove] = new Point(e.X, e.Y);
                draw();
                mouseMove = true;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!mode)
            {
                if (!isDrawn) return;
                mouseMove = false;
                mouseDown = false;
                dataGridView1.Rows[pointMove].Cells[0].Value = points[pointMove].X.ToString();
                dataGridView1.Rows[pointMove].Cells[1].Value = points[pointMove].Y.ToString();
                toList();
            }
        }

        private void changeColor(int x, int y)
        {
            int point = -1;
            for (int i = 0; i < points.Count; i++)
            {
                int predicate = (x - points[i].X) * (x - points[i].X) + (y - points[i].Y) * (y - points[i].Y);
                if (predicate <= 100)
                {
                    point = i;
                    break;
                }
            }
            if (point < 0) return;
            switch (colors[point].Name)
            {
                case "Black":
                    dataGridView1.Rows[point].Cells[2].Value = "Red";
                    break;
                case "Red":
                    dataGridView1.Rows[point].Cells[2].Value = "Green";
                    break;
                case "Green":
                    dataGridView1.Rows[point].Cells[2].Value = "Blue";
                    break;
                case "Blue":
                    dataGridView1.Rows[point].Cells[2].Value = "Black";
                    break;
            }
            DrawButton.PerformClick();
        }

        private void pointLeftClick(int x, int y)
        {
            int point = -1;
            for (int i = 0; i < points.Count; i++)
            {
                int predicate = (x - points[i].X) * (x - points[i].X) + (y - points[i].Y) * (y - points[i].Y);
                if (predicate <= 100)
                {
                    point = i;
                    break;
                }
            }
            if (point < 0) addPoint(x, y);
            else
            {
                if (!connect)
                {
                    connect = true;
                    connectPoint = point;
                }
                else
                {
                    if (dataGridView1.Rows[connectPoint].Cells[3].Value != null)
                    {
                        dataGridView1.Rows[connectPoint].Cells[3].Value += "," + (point + 1).ToString();
                    }
                    else
                    {
                        dataGridView1.Rows[connectPoint].Cells[3].Value = (point + 1).ToString();
                    }
                    connect = false;
                }
            }
            DrawButton.PerformClick();
        }

        private void pointRightClick(int x, int y)
        {
            int point = -1;
            for (int i = 0; i < points.Count; i++)
            {
                int predicate = (x - points[i].X) * (x - points[i].X) + (y - points[i].Y) * (y - points[i].Y);
                if (predicate <= 100)
                {
                    point = i;
                    break;
                }
            }
            if (point < 0) return;
            if (!ctrl) removePoint(point);
            else
            {
                if (!deleteLine)
                {
                    delete = point;
                    deleteLine = true;
                }
                else
                {
                    removeLine(point + 1, delete);
                    removeLine(delete + 1, point);
                    deleteLine = false;
                }
            }
            DrawButton.PerformClick();
        }

        private void removeLine(int point, int where)
        {
            if (dataGridView1.Rows[where].Cells[3].Value == null) return;
            dataGridView1.Rows[where].Cells[3].Value =
                dataGridView1.Rows[where].Cells[3].Value.ToString().Replace("," + point, "");
            dataGridView1.Rows[where].Cells[3].Value =
                dataGridView1.Rows[where].Cells[3].Value.ToString().Replace(point + ",", "");
            dataGridView1.Rows[where].Cells[3].Value =
                dataGridView1.Rows[where].Cells[3].Value.ToString().Replace(point.ToString(), "");
        }


        private void removePoint(int point)
        {
            var a = MessageBox.Show("Do you really want to delete this point?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (a == DialogResult.No) return;
            dataGridView1.Rows.RemoveAt(point);
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    if (row.Cells[3].Value != null)
                    {
                        row.Cells[3].Value = Worker.parseToChange(row.Cells[3].Value.ToString(), point);
                    }
                }
            }
        }

        private void addPoint(int x, int y)
        {
            int row = dataGridView1.Rows.Add(1);
            dataGridView1.Rows[row].HeaderCell.Value = (row + 1).ToString();
            dataGridView1.Rows[row].Cells[0].Value = x.ToString();
            dataGridView1.Rows[row].Cells[1].Value = y.ToString();
            dataGridView1.Rows[row].Cells[2].Value = "Black";
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!mode)
            {
                if (e.Button == MouseButtons.Right)
                {
                    changeColor(e.X, e.Y);
                }
            }
            else
            {
                if (e.Button == MouseButtons.Left)
                {
                    pointLeftClick(e.X, e.Y);
                }
                if (e.Button == MouseButtons.Right)
                {
                    pointRightClick(e.X, e.Y);
                }
            }
        }

    }
}
